<?php
namespace YzPay\Controller\Home;
//use Think\Controller;
class IndexController extends CommonController {
	public function _initialize() {
		parent::_initialize();

	}
	function test() {
		//支付宝配置 沙盒号
		$config = array(
			'appId' => '2016072900116809',
			'rsaPrivateKey' => 'MIICXAIBAAKBgQDC3YX1bmYtwWy4E1s6NZ1BYLr+neEqla6j8vVJVs9mH+RGa4dyw+6hJbFdKgsLrn9GdDHL6V9XcxunEVYx3qC1STmsgxUw4xjz2nbtNqzWYChi8i8lwVnc72ENgJrreSPNKLx1IeINIpvnnMEn8EHY7YK14wV2t/Djjr/iMfqboQIDAQABAoGAKhgKUPs5LVckMhtUUJ22S6ImosveEQHmiNn57I7JHIl1GVUvHDaA/OSUjMiBPFPEgKam/yJ5iv19krhnAzzRQBd6sxCLlaxF/RYZP5A/rjj/TQnyq7eV2o6Fgq3jFjJHt675zvd7bNyzPgYuy60z2nTLbszydSCdbJfFY3Ika7UCQQDlWrwxgDeyugLq3iKqlNLEfRD4Sm1TDm65v8tswXnK9YEfgy94LGyVMAU/tPFF5L+a9DGbfn/xgeLkX09QEaV3AkEA2YEK14dMe0IliZ8EkP1n1FmHzfUJi9Ty10pB7PmvNcAHKeikMfmvYM8Xl4f5wyCwpZPShHGkkZ8ZdCWZ9hxtpwJAGNgCSK5l2iNSC5TyVl32krglbW/KddO7ZL2E0IwIoFaFnR89thvBy0s/Vk5IUFTd+G/5K6nBPCzgofI9CiRPMwJBAMxqK99dyDJ7ez35FliR4DhBoJGfPsaNb2GI56apDtMMU28+trpFOF9aooiGgeSyX3JuGEbja1B03346LJWSKDcCQEv0ux2T7Kw3qT+1nGY9pVgPBNJ+1vm5StNuCLvhUO44jH3sf18Zikuym++Z+GBpz5XU2WUOJQcur1hLSI+mY6E=',
			'alipayrsaPublicKey' => 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB',
		);
		$payClass = new \Common\Org\YzPay\YzPay($config);

		$params = array(
			'out_trade_no' => '20150320010101005',
			'total_amount' => '99',
			'subject' => '订单标题',
		);
		$req = $payClass->QRCode($params);

		echo '<img src="/BoxApi/Index/QRcode?url=' . $req['alipay_trade_precreate_response']['qr_code'] . '" class="fd-left">';
		dump($req);

	}
	/******我的****/
	public function index() {
		if (I('box_login_code')) {
			$this->jump("");
		}
		$this->display();

	}
	//向商家支付
	public function pay() {
		$user_id = I('user_id');
		$sign = I('sign');
		$this->money = is_numeric(I('money')) ? I('money') : 0;
		$this->data = $data = BoxModel("yz_member")->load($user_id);
		$row = array('user_id' => $data['user_id'], 'name' => $data['name'], 'openid' => $data['openid'], 'store_name' => $data['store_name']);
		ksort($row);
		$sign2 = thinkEncrypt($row);
		if ($sign != $sign2) {
			$this->jump("当前二维码已失效，请重新生成", U('User/index'));
		}
		$this->web_title = "向商家付款";
		if ($this->money) {
			$model = BoxModel("yz_member");
			$model->startTrans(); //开启事务
			//生成订单去支付
			$row = array(
				'store_id' => 1,
				'remark' => I('remark'),
				'user_id' => $this->user_id,
				'openid' => $this->user_info['openid'],
				'payment_id' => intval(I('payment_id')),
				'pay_user_id' => $data['user_id'],
				'pay_openid' => $data['openid'],
				'order_amount' => $this->money,
				'goods_amount' => $this->money,
				'order_type' => 'yzpay',
				'create_time' => time(),
				'suceess_url' => yzpayUrl . "/User/order/pay/1",
				'title' => '向商家：' . $data['store_name'] . "付款",
				'body' => '向商家：' . $data['store_name'] . "付款",
			);
			$r = Logic("BoxMall", "Order")->add($row);
			//	dump($row);exit();
			$order_sn = $r['order_sn'];
			$GLOBALS['pay_order_sn'] = $order_sn;
			if (!$r) {
				$model->rollback();
				$this->jump("系统忙，请重试");
			} else {
				$model->commit();
				$this->jump("", HomeUrl . "/BoxPay/Order/index/order_sn/" . $order_sn . ".html");
			}
			exit();
		}

		$this->display();
	}

}